package top.lingkang.sprites;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.g2d.*;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.*;
import top.lingkang.CoolRunGame;
import top.lingkang.constants.FixtureDefType;
import top.lingkang.screen.PlayScreen;

public class PlayerSprite extends Sprite {
    private PlayScreen playScreen;
    private Animation<TextureRegion> animation;
    private TextureAtlas atlas;
    private SpriteBatch batch;
    private float stateTime;

    private World world;
    public Body body;
    public Vector2 position;
    public boolean isJump;
    public boolean airJump;


    public PlayerSprite(PlayScreen playScreen, World world, float x, float y) {
        this.playScreen = playScreen;
        this.batch = playScreen.batch;
        this.world = world;
        position = new Vector2(x / CoolRunGame.PPM, y / CoolRunGame.PPM);

        atlas = new TextureAtlas(Gdx.files.internal("roles/role_1/role_1.atlas"));
        TextureRegion[] textureRegions = new TextureRegion[11];
        for (int i = 0; i < 11; i++) {
            textureRegions[i] = new TextureRegion(atlas.getRegions().get(i));
        }
        animation = new Animation<TextureRegion>(1 / 11f, textureRegions);

        // 初始化物体
        initCurrentBox2D();
        Gdx.input.setInputProcessor(new InputAdapter() {
            @Override
            public boolean keyDown(int keycode) {
                if (keycode == Input.Keys.SPACE) {
                    if (!isJump) {
                        isJump = true;
                        body.applyLinearImpulse(new Vector2(0, 8f), body.getWorldCenter(), true);
                    } else if (!airJump) {
                        airJump = true;
                        body.setLinearVelocity(body.getLinearVelocity().x, 0);// 将重力置为 0 再起跳
                        body.applyLinearImpulse(new Vector2(0, 6f), body.getWorldCenter(), true);
                    }
                }
                return super.keyDown(keycode);
            }
        });

    }

    public void update(float delta) {
        stateTime += delta;
        position = body.getPosition();
        batch.draw(animation.getKeyFrame(stateTime, true),
                position.x - 103 / 2 / CoolRunGame.PPM,
                position.y - 111 / 2 / CoolRunGame.PPM,
                0, 0, 103, 111,
                1 / CoolRunGame.PPM, 1 / CoolRunGame.PPM,
                0);

        body.setLinearVelocity(new Vector2(playScreen.moveSpeed, body.getLinearVelocity().y));

    }

    private void initCurrentBox2D() {
        // 再添加一个动态物体，可以把他看成玩家
        BodyDef dogBodyDef = new BodyDef();
        dogBodyDef.type = BodyDef.BodyType.DynamicBody;
        dogBodyDef.position.x = position.x;
        dogBodyDef.position.y = position.y;
        body = world.createBody(dogBodyDef);
        PolygonShape dynamicBox = new PolygonShape();
        dynamicBox.setAsBox(103 / 2 / CoolRunGame.PPM, 111 / 2 / CoolRunGame.PPM);

        // 给物体添加一些属性
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = dynamicBox;// 形状
        fixtureDef.filter.categoryBits = FixtureDefType.PLAYER;
        //fixtureDef.restitution = 0.2f; // 设置这个值后，物体掉落到地面就会弹起一点高度...
        body.createFixture(fixtureDef).setUserData(this);//设置自定义数据可以从这个物体获取这个数据对象
        dynamicBox.dispose();
    }

    public void dispose() {
        batch.dispose();
        atlas.dispose();
    }
}
